/*
 ----------------------------------------------------------------------------------
                          "THE BEER-WARE COPYRIGHT LICENSE"
                                    (Revision 43)
                                     -----------
   Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
   this file has been sent directly to You by the author, You can do whatever You
   want with this stuff as long as You don't sell or rent it. If we meet some day,
   and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                __________
                                                                Paweł Rymaszewski
 ----------------------------------------------------------------------------------
 */

package ui.windows.components.panels.messageInformationPanel;

import processing.logic.util.Mapper;
import processing.objects.Message;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 * Handles list changes on the MessageList combobox
 *
 * @author rymek
 *         User: Rymek
 *         Date: 20.07.11
 *         Time: 22:56
 */
class MessageListItemListener implements ItemListener {

    private final MessageInformationPanel messageInformationPanel;            // parent MessageInformationPanel instance

    /**
     * Consstructor.
     * Creates reference to parent panel.
     *
     * @param messageInformationPanel parent panel
     */
    public MessageListItemListener(MessageInformationPanel messageInformationPanel) {
        this.messageInformationPanel = messageInformationPanel;
    }

    /**
     * Handles state changes in Message List ComboBox
     *
     * @param e The Event
     */
    @Override
    public void itemStateChanged(ItemEvent e) {

        if (this.messageInformationPanel.messageSelected()) {
            this.messageInformationPanel.getMsgEffDate().setText("");
            this.messageInformationPanel.getMsgServiceName().setText("");
            this.messageInformationPanel.getMsgRequestName().setText("");
            this.messageInformationPanel.getMsgResponseName().setText("");
            this.messageInformationPanel.getMsgTrxType().setText("");
            this.messageInformationPanel.getMsgNodeName().setText("");
            this.messageInformationPanel.getMsgChannelName().setText("");
            this.messageInformationPanel.getActive().setSelected(false);
            this.messageInformationPanel.getCustomPublisher().setSelected(false);

        } else {

            Message message = this.messageInformationPanel.getDbMessages().get(this.messageInformationPanel.getMessageList().getSelectedItem().toString());

            this.messageInformationPanel.getMsgEffDate().setText(message.getEffectiveDate());
            this.messageInformationPanel.getMsgTrxType().setText(Mapper.mapTransactionType(message.getTransactionType()));
            this.messageInformationPanel.getMsgServiceName().setText(message.getServiceName());
            this.messageInformationPanel.getMsgRequestName().setText(message.getRequestName());
            this.messageInformationPanel.getMsgResponseName().setText(message.getResponseName());
            this.messageInformationPanel.getMsgNodeName().setText(message.getNodeName());
            this.messageInformationPanel.getMsgChannelName().setText(message.getChannelName());
            this.messageInformationPanel.getActive().setSelected(message.isActive());
            this.messageInformationPanel.getCustomPublisher().setSelected(message.isCustomPublisher());
        }
    }
}
